#include "MenuItem.h"
#include "../std.h"

MenuItem::MenuItem(int x, int y, int width, int height, Widget* parent)
	   : Widget(x, y, width, height, parent),
	     mNextImage(NULL),
	     mListening(false)
{
	mTitle = new Label(0, 0, width, height, this);
	mTitle->setDrawBackground(false);
	mTitle->setMultiLine(true);
	mTitle->setAutoSizeX(false);
	mTitle->setAutoSizeY(false);
	mTitle->setPaddingLeft(VERTICALPADDING);
	mTitle->setPaddingRight(VERTICALPADDING);
	mTitle->setPaddingBottom(HORIZONTALPADDING);
	mTitle->setPaddingTop(HORIZONTALPADDING);
}

MenuItem::~MenuItem()
{
	if(mListening)
		Environment::getEnvironment().removePointerListener(this);
}

void MenuItem::setTitle(String title)
{
	mTitle->setCaption(title);
	mTitle->update();
	this->setHeight(mTitle->getHeight());
}

void MenuItem::setTitleFont(Font* font)
{
	mTitle->setFont(font);
	mTitle->update();
	this->setHeight(mTitle->getHeight());
}

//Makes sure that the next image is in the correct place.  Ensure that you've sent the title
//and description text and fonts before you set this image, so that it can be placed correctly.
void MenuItem::setNextImage(MAHandle nextImage)
{
	//
	if(mNextImage == NULL)
		mNextImage = new Image(0, 0, 16 + VERTICALPADDING*2, 16 + HORIZONTALPADDING * 2, this, false, false, nextImage);
	else
		mNextImage->setResource(nextImage);

	mNextImage->setPaddingLeft(VERTICALPADDING);
	mNextImage->setPaddingRight(VERTICALPADDING);
	mNextImage->setPaddingBottom(HORIZONTALPADDING);
	mNextImage->setPaddingTop(HORIZONTALPADDING);

	//Updates the dimensions of the image
	mNextImage->update();

	//Set the size of the text boxes
	mTitle->setWidth(this->getWidth() - mNextImage->getWidth());

	//Updates the dimensions of the text boxes
	mTitle->update();

	//Put the image to the right of the text boxes, and aligned with the title
	mTitle->setPosition(mNextImage->getWidth(), 0);
	//mNextImage->setPosition(this->getWidth() - mNextImage->getWidth(), 0);
}

const String& MenuItem::getTitle()
{
	return mTitle->getCaption();
}

void MenuItem::setSelected(bool selected)
{
	/*if(selected)
	{
		this->setHeight(mTitle->getHeight() + mText->getHeight());
		mText->setPosition(0, mTitle->getHeight());
	}
	else
		this->setHeight(mTitle->getHeight());*/

	Widget::setSelected(selected);

	if(selected)
	{
		if(!mListening)
		{
			Environment::getEnvironment().addPointerListener(this);
			mListening = true;
		}
	}
	else
	{
		if(mListening)
		{
			Environment::getEnvironment().removePointerListener(this);
			mListening = false;
		}
	}
	this->getParent()->update();
}

void MenuItem::pointerPressEvent(MAPoint2d p)
{}

void MenuItem::pointerMoveEvent(MAPoint2d p)
{}

void MenuItem::pointerReleaseEvent(MAPoint2d p)
{
	/*mNextImage->update();
	KineticListBox* klb = (KineticListBox*)this->getParent();
	Point testpos = mNextImage->getPosition();

	//If you reuse this component, and your widget hierarchy is different, then you
	//will need to recalculate this.
	int adjustedY = testpos.y + this->getPosition().y + this->getParent()->getPosition().y + (klb->getYOffset() >> 16);

	//Inform WidgetListeners if the next item is pressed
	if(mNextImage != NULL)
		if(p.x >= testpos.x && p.x <= testpos.x + mNextImage->getWidth()
		&& p.y >= adjustedY && p.y <= adjustedY + mNextImage->getHeight())
			trigger();*/
}

//Make the appropriate widgets visible when selected.
void MenuItem::drawWidget()
{
	//mNextImage->setEnabled(this->isSelected());
	update();
}
